package com.yy.wga.goods.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.yy.wga.common.core.dto.PageBean;
import com.yy.wga.common.core.enmus.EUserGrade;
import com.yy.wga.common.mysql.util.PageUtil;
import com.yy.wga.common.util.mapping.BeansUtils;
import com.yy.wga.common.web.intereceptor.RequestHolder;
import com.yy.wga.goods.feign.request.GoodsPageRequest;
import com.yy.wga.goods.feign.request.GoodsUpGradeInfoRequest;
import com.yy.wga.goods.feign.response.GoodsInfoPageResponse;
import com.yy.wga.goods.feign.response.GoodsUpGradeInfoResponse;
import com.yy.wga.goods.service.entity.pojo.GoodsUpGradeDO;
import com.yy.wga.goods.service.mapper.GoodsUpGradeMapper;
import com.yy.wga.goods.service.service.GoodsUpGradeService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * spu信息(GoodsSpu)表服务实现类
 *
 * @author guochao
 * @since 2024-11-06 21:54:15
 */
@Service
@RequiredArgsConstructor
public class GoodsUpGradeServiceImpl extends ServiceImpl<GoodsUpGradeMapper, GoodsUpGradeDO> implements GoodsUpGradeService {

    @Override
    public PageBean<GoodsInfoPageResponse> pageData(GoodsPageRequest request) {
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        List<GoodsUpGradeDO> goodsSpuList = baseMapper.selectList(new LambdaQueryWrapper<GoodsUpGradeDO>()
                .eq(GoodsUpGradeDO::getAppKey, RequestHolder.getBaseRequest().getAppKey())
                .eq(request.getId() != null, GoodsUpGradeDO::getId, request.getId()));
        if (CollectionUtils.isEmpty(goodsSpuList)) {
            return new PageBean<>();
        }

        // 封装实体
        PageBean<GoodsInfoPageResponse> pageBean = PageUtil.setPageInfo(goodsSpuList)
        pageBean.setList(BeansUtils.mapList(goodsSpuList, GoodsInfoPageResponse.class));
        return pageBean;
    }

    @Override
    public List<GoodsUpGradeInfoResponse> listGoods(GoodsUpGradeInfoRequest request) {
        PageHelper.startPage(request.getPageNum(), request.getPageSize(), false);
        List<GoodsUpGradeDO> goodsUpGradeList = baseMapper.selectList(new LambdaQueryWrapper<GoodsUpGradeDO>()
                .eq(GoodsUpGradeDO::getAppKey, RequestHolder.getBaseRequest().getAppKey())
                .eq(GoodsUpGradeDO::getStatus, 1)
                .orderByDesc(GoodsUpGradeDO::getSort));
        if (!CollectionUtils.isEmpty(goodsUpGradeList)) {
            List<GoodsUpGradeInfoResponse> responseList = new ArrayList<>();
            UserCacheDTO userCache = RequestUserHolder.getUserCache();
            for (GoodsUpGradeDO goodsUpGrade : goodsUpGradeList) {
                // 等级不足不展示
                if (userCache.getGrade() < goodsUpGrade.getPurchaserMinGrade()) {
                    continue;
                }
                if (userCache.getGrade().equals(goodsUpGrade.getPurchaserMinGrade())
                        && userCache.getLevel() < goodsUpGrade.getPurchaserMinLevel()) {
                    continue;
                }
                // 等级小于升级等级，展示原价
                GoodsUpGradeInfoResponse response = BeansUtils.map(goodsUpGrade, GoodsUpGradeInfoResponse.class);
                response.setUpgrade(EUserGrade.getByType(goodsUpGrade.getUpGrade(), goodsUpGrade.getUpGradeLevel()).getDesc());
                if (userCache.getGrade() < goodsUpGrade.getUpGrade()) {
                    responseList.add(response);
                    continue;
                }
                if (userCache.getGrade().equals(goodsUpGrade.getUpGrade())
                        && userCache.getLevel() < goodsUpGrade.getUpGradeLevel()) {
                    responseList.add(response);
                    continue;
                }
                // 复购的话设置为复购价格
                if (userCache.getGrade() > goodsUpGrade.getUpGrade()) {
                    if (userCache.getGrade().equals(EUserGrade.SHARE_3.getType())) {
                        response.setFinalPrice(goodsUpGrade.getShareRepurchaseFinalPrice());
                    } else {
                        response.setFinalPrice(goodsUpGrade.getServerRepurchaseFinalPrice());
                    }
                }
                if (userCache.getGrade().equals(goodsUpGrade.getUpGrade())
                        && userCache.getLevel() >= goodsUpGrade.getUpGradeLevel()) {
                    if (userCache.getGrade().equals(EUserGrade.SHARE_3.getType())) {
                        response.setFinalPrice(goodsUpGrade.getShareRepurchaseFinalPrice());
                    } else {
                        response.setFinalPrice(goodsUpGrade.getServerRepurchaseFinalPrice());
                    }
                }
                responseList.add(response);
            }
            if (Boolean.TRUE.equals(request.getLast()) && !responseList.isEmpty()) {
                responseList = responseList.subList(0, 1);
            }
            return responseList;
        }
        return Collections.emptyList();
    }
}
